function [ A_monot ] = A_monot_fun_2(combos_all_unique,J)

%% sigma is increasing in: own x, own z
%%          decreasing in: other x other z

n_Bcoeff = size(combos_all_unique,1);

mat_minus = cell(n_Bcoeff,1);
mat_plus = cell(n_Bcoeff,1);

sub_minus = cell(n_Bcoeff,1);
sub_plus = cell(n_Bcoeff,1);

parfor c=1:n_Bcoeff
    
    temp_ind = perms(1:J-1);    % all permutations for x_other (and z_other)
    
    %% Given combo is worse
    
    set_temp = [];
    
    for i=1:size(temp_ind,1)
        for j=1:size(temp_ind,1)
            
            temp_x_other = combos_all_unique(c,2:J);
            temp_x_other_reord = temp_x_other(temp_ind(i,:));
            
            temp_z_other = combos_all_unique(c,J+2:2*J);
            temp_z_other_reord = temp_z_other(temp_ind(i,:));
            
            temp_vec = [combos_all_unique(c,1)   temp_x_other_reord...
                        combos_all_unique(c,J+1) temp_z_other_reord];
            
            temp_ind_x = [2:J];
            temp_ind_x_reord = temp_ind_x(temp_ind(j,:));
            temp_ind_z = [J+2:2*J];
            temp_ind_z_reord = temp_ind_z(temp_ind(j,:));
            combos_all_unique_reord = combos_all_unique(:,[1 temp_ind_x_reord J+1 temp_ind_z_reord]);
            
            temp_geq = all(repmat(temp_vec([1 J+1]),[size(combos_all_unique,1),1])<=combos_all_unique_reord(:,[1 J+1]),2);             % take combos with HIGHER own x, own z
            temp_leq = all(repmat(temp_vec([2:J J+2:2*J]),[size(combos_all_unique,1),1])>=combos_all_unique_reord(:,[2:J J+2:2*J]),2);     % take combos with LOWER other x, other z
            temp_final = logical(temp_geq.*temp_leq);
            set_temp = [set_temp; find(temp_final)];
            
        end
    end
    
    sub_minus{c} = unique(set_temp);
    sub_minus{c} = setdiff(sub_minus{c},c);
    
    %% Given combo is better
    
    set_temp = [];
    
    for i=1:size(temp_ind,1)
        for j=1:size(temp_ind,1)
            
            temp_x_other = combos_all_unique(c,2:J);
            temp_x_other_reord = temp_x_other(temp_ind(i,:));
            
            temp_z_other = combos_all_unique(c,J+2:2*J);
            temp_z_other_reord = temp_z_other(temp_ind(i,:));
            
            temp_vec = [combos_all_unique(c,1)   temp_x_other_reord...
                        combos_all_unique(c,J+1) temp_z_other_reord];
            
            temp_ind_x = [2:J];
            temp_ind_x_reord = temp_ind_x(temp_ind(j,:));
            temp_ind_z = [J+2:2*J];
            temp_ind_z_reord = temp_ind_z(temp_ind(j,:));
            combos_all_unique_reord = combos_all_unique(:,[1 temp_ind_x_reord J+1 temp_ind_z_reord]);
            
            temp_geq = all(repmat(temp_vec([1 J+1]),[size(combos_all_unique,1),1])>=combos_all_unique_reord(:,[1 J+1]),2);             % take combos with LOWER own x, own z
            temp_leq = all(repmat(temp_vec([2:J J+2:2*J]),[size(combos_all_unique,1),1])<=combos_all_unique_reord(:,[2:J J+2:2*J]),2);     % take combos with HIGHER other x, other z
            temp_final = logical(temp_geq.*temp_leq);
            set_temp = [set_temp; find(temp_final)]
            
        end
    end
    
    sub_plus{c} = unique(set_temp);
    sub_plus{c} = setdiff(sub_plus{c},c);
    
end

parfor c=1:n_Bcoeff
    
    sub_minus_union = [];
    
    for i=1:length(sub_minus{c})
        
        sub_minus_union = union(sub_minus_union,sub_minus{sub_minus{c}(i)});
        
    end
    
    %% MINUS

    temp_sub_col_minus = sub_minus{c};%setdiff(sub_minus{c},sub_minus_union);             % eliminate redundant inequalities, i.e. lines (because of transitivity)
    temp_sub_row_minus = [1:length(temp_sub_col_minus)]';
    temp_mat = zeros(length(temp_sub_col_minus),n_Bcoeff);
    
    if isempty(temp_sub_col_minus)==0
        
    temp_ind_minus = sub2ind(size(temp_mat),temp_sub_row_minus,temp_sub_col_minus);
    
    temp_mat(temp_ind_minus) = -1;
    
    temp_sub_col_plus = c*ones(length(temp_sub_col_minus),1);
    temp_sub_row_plus = [1:length(temp_sub_col_minus)]';
    
    temp_ind_plus = sub2ind(size(temp_mat),temp_sub_row_plus,temp_sub_col_plus);
    
    temp_mat(temp_ind_plus) = +1;
    
    %temp_mat((temp_sub_col_minus==c),:) = [];     % eliminate row for c; dropping this does not make a difference
    
    end
    
    mat_minus{c} = sparse(temp_mat);
    
    %% PLUS
    
    temp_sub_col_plus = sub_plus{c};%setdiff(sub_minus{c},sub_minus_union);             % eliminate redundant inequalities, i.e. lines (because of transitivity)
    temp_sub_row_plus = [1:length(temp_sub_col_plus)]';
    temp_mat = zeros(length(temp_sub_col_plus),n_Bcoeff);
    
    if isempty(temp_sub_col_plus)==0
        
    temp_ind_plus = sub2ind(size(temp_mat),temp_sub_row_plus,temp_sub_col_plus);
    
    temp_mat(temp_ind_plus) = +1;
    
    temp_sub_col_minus = c*ones(length(temp_sub_col_plus),1);
    temp_sub_row_minus = [1:length(temp_sub_col_plus)]';
    
    temp_ind_minus = sub2ind(size(temp_mat),temp_sub_row_minus,temp_sub_col_minus);
    
    temp_mat(temp_ind_minus) = -1;
    
    %temp_mat((temp_sub_col_plus==c),:) = [];     % eliminate row for c; dropping this does not make a difference
    
    end
    
    mat_plus{c} = sparse(temp_mat);
    
    %n_Bcoeff-c
end

A_monot_minus = cell2mat(mat_minus);
A_monot_plus = cell2mat(mat_plus);

A_monot = [A_monot_minus;
           A_monot_plus];

A_monot = unique(A_monot,'rows');

end